dl_manager.admin = liwe.module ( "dl_manager.admin", null, "DL Manager" );
dl_manager.admin.dest_div = 'DLM-upload-form';

dl_manager.admin.init = function ()
{
	if ( ! dl_manager.templates )
	{
		console.debug ( "no templates" );
		dl_manager.load_templates ( dl_manager.admin.init );
		return;
	}
        // ===================================================================
        // PRIVATE ATTRIBUTES
        // ===================================================================
	// init vars
        var _acts = [
                        { func: function () 
				{ 
					var attrs = { 'dest_div' : 'system-content' } ; 
					dl_manager.render ( 'dl_manager', attrs ); 
				}, 
				img_src: '', 
				label: 'Gestione Files', "class" : "dm" }
                ];

        var _plugin = [
                {
                        label:   "DL Manager",
                        handler: dl_manager.admin._jconed_register_handler,
                        context: dl_manager.admin._jconed_context
                }
        ];

        dl_manager.admin.register ( _acts, _plugin );
};

/* controlla campi form
	- il valore 'action' per ajax deve essere in un campo hidden del form
	- al termine dell'invio chiude il div contenente il form e aggiorna la ricerca
	FIXME:
		 - il form di upload non passa  l'oggetto uploader non viene passato
*/

dl_manager.admin._chk_form = function ( form_id )
{
	if ( ! liwe.form.check ( form_id ) ) 
	{
		console.debug ( "%s : check fallito", form_id );
		return;
	}

	var f = liwe.form.get ( form_id );
	var vals = f.get_values();
	var name = vals.get ( 'inst' , '' );
	console.debug ( vals );
	var inst = dl_manager.get_instance ( name );
	if ( ! inst ) return;
	dl_manager.ajax ( vals , function ( data ) 
					{
						inst.cbacks [ 'upload_form' ] ( inst.upload_form_id );
						dl_manager.browse_files ( name );
					} );
};

dl_manager.admin.upload_form = function ( name )
{
	var inst = dl_manager.get_instance ( name );
	var dest_div = inst.upload_form_id;
	var f = new liwe.form.instance ( inst.upload_form_name );

	f.events [ 'complete' ] = function () { dl_manager.admin._complete ( frm ); }; //note: don't change param name ( frm ). It is defined in 'form' library

	f.hidden ( "action", "dl_manager.admin.add_file" );
	f.hidden ( "id_obj", inst [ 'id_obj' ] );
	f.hidden ( "inst", name );
	f.select ( { name: "module", options: dl_manager._mods_list, force_select: false, style: "width:150px;",
			value: inst.module } ); //onchange: "dl_manager.set_module ( this.value );" } );
	//f.hidden ( "module", dl_manager._module );
	f.hidden ( "__PYHP_SESSION", liwe.cookie.get ( "session" ) );

	f.label ( { value: "Rinomina file" } );
	f.text ( { name: "name", maxlength: 200, filter: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_."} );
	f.label ( { value: "Descrizione" } );
	//f.textarea ( { name: "descr", cols: 20, rows: 4 } );
	f.jconed ( { name: "descr", width: 150, height: 50, value : '' } );
	f.label ( { value: "Upload" } );
	f.uploader ( { name: "upload" } );
	f.label ( { value: "Tags" } );
	f.tags ( { name: "files_tags", module_name: 'dl_manager', size: '20' } ); //dl_manager._module
	f.submit ( { value: "Invia", nonl: true, "class": "green" } ); 
	f.set ( dest_div );
};

dl_manager.admin._complete = function ( frm_obj )
{
	var params = frm_obj.get_values ();
	var name = params.get ( 'inst' );
	if ( ! name ) 
		console.warn ( "DL_MANAGER: missing 'inst' field in upload form.\n" );

	var inst = dl_manager.get_instance ( name )
	dl_manager.browse_files ( name, params );
	inst.cbacks [ 'upload_form' ] ( name ); 
};

dl_manager.admin.edit_form = function ( name )
{
	var inst = dl_manager.get_instance ( name );
	if ( ! inst ) return; 

	var dest_div = inst.upload_form_id;

	var f = new liwe.form.instance ( inst.edit_form_name );
	
	f.hidden ( "action", "dl_manager.admin.add_file" );
	f.hidden ( "update", "update"  );
	f.hidden ( "id_file", ""  );
	f.hidden ( "id_obj", inst [ 'id_obj' ]  );
	f.hidden ( "inst", name );
	f.hidden ( "module", inst.module );
	f.label ( { value: 'Rinomina file' } );
	f.text ( { name: "name", maxlength: 200, filter: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_."} );
	f.label ( { value: 'Descrizione' } );
	f.jconed ( { name: "descr", width: 150, height: 50, value : '' } );
	f.label ( { value: 'Tags' } );
	f.tags ( { name: "files_tags", module_name: 'dl_manager', size: '20' } ); //dl_manager._module
	f.button ( { value: "Salva modifiche", "class": "green", onclick: "dl_manager.admin._chk_form ( '" + inst.edit_form_name + "') " } );
	f.button ( { value: "Carica nuovo file", "class": "blue", onclick: "dl_manager.admin._upload_form( '" + inst.edit_form_name + "')" } );

	f.set ( dest_div );
};

// keep id_obj value if it has been set
dl_manager.admin._upload_form = function ( form_id )
{
	var f = liwe.form.get ( form_id );
	var vals = f.get_values ();
	var name = vals.get ( 'inst' );
	if ( ! inst ) 
		console.warn ( "DL_MANAGER: missing 'inst' field in upload form.\n" );
	var inst = dl_manager.get_instance ( name )
	inst.cbacks [ 'upload_form' ] ( name ); 
};

dl_manager.admin._set_form_values = function ( name, data, cback )
{
	var inst = dl_manager.get_instance ( name );
	if ( ! inst ) return;

	var val, f = liwe.form.get ( inst.edit_form_name );
	f._fields.iterate ( function ( field )
		{
			val = data [ 'res' ].get ( field );
			console.debug ( "field: %s - val: %s" , field , val );
			if ( ! val || val == '' ) return;
			f.set_value ( field, val );
		} );

	if ( cback ) cback ();
};

dl_manager.admin.edit_file = function ( name, id, update, id_obj, cback )
{
	var inst = dl_manager.get_instance ( name );
	if ( ! inst ) return

	inst.cbacks [ 'edit_form' ] ( name );
	var vals = {
			"action": "dl_manager.admin.edit_file", 
			"id_file" : id, 
			"update" : update, 
			"id_obj" : id_obj,
			"module" : inst.module
			};
	dl_manager.ajax ( vals, function ( data ) 
		{
			dl_manager.admin._set_form_values ( name, data, cback );
			inst.tabs.send_event ( inst.upload_form_id, 'click' );
		}  );
};

dl_manager.admin.remove_file = function ( name, path_name, id, id_obj )
{
	var inst = dl_manager.get_instance ( name );
	if ( ! name ) return;

	module = inst.module;

	if ( path_name && id ) 
	{
		var uconfirm = confirm ( "Vuoi eliminare il file selzionato ?" );
		if ( uconfirm )
		{
			var vals = {
				"action" : "dl_manager.admin.remove_file", 
				"path_name" : path_name, 
				"id_file" : id, 
				"id_obj" : id_obj,
				"module" : module
				};
			dl_manager.ajax ( vals, function ( data )
							{
								dl_manager.browse_files ( name );
							} );
		}
	}
};
/* JCONED PLUGIN FUNCTIONS */
dl_manager.admin._jconed_register_handler = function ( data ) 
{
	dl_manager.admin._jconed_form ();
};

dl_manager.admin._jconed_form = function ( frm_data ) 
{
	var name = 'jconed';
	var attrs = { 'dest_div' : 'jconed-dl-box-preview' };
	var inst = dl_manager.init_instance ( name, attrs );
	
	if ( ! dl_manager._ext_list || !  dl_manager._mods_list )
	{
		dl_manager._get_options ( name, function ()
			{
				dl_manager.admin._jconed_form ( frm_data );
			});	
	}
	var view_opts = [
				{ 'label' : 'icons', 'value' : 'icons' },
				{ 'label' : 'list', 'value' : 'list' },
				{ 'label' : 'details', 'value' : 'details' }
			];

        var f = new liwe.form.instance ( 'jconed-dl-box-form', 'no-action' ); 
 
        // create filter form 
	f.hidden ( { 'name' : 'name', 'value' : name } );
	f.select ( { label: 'Modulo', name: "module", options: dl_manager._mods_list, force_select: false, style: "width:150px;", nonl : true,
		value: inst.module, onchange: "dl_manager.set_module ( '" + name + "', this.value );" } );
	f.select ( { name: "view", options: view_opts, force_select: false, style: "width:150px;",
		value: dl_manager._module, onchange: "dl_manager.set_module ( '" + name + "', this.value );" } );
	//f.select ( { name: "module", options: dl_manager._mods_list, force_select: false, style: "width:150px;",
	//		onchange: function () { dl_manager.set_module ( '', 'jconed-dj-box-form' ); } } );
        f.text ( { label: 'ID elemento', name : "id_obj", nonl : true } ); 
        f.text   ( { label: "Nome file", name: "name", maxlength: 200 } ); 
        f.text   ( { label: "Descrizione", name: "descr", maxlength: 200, nonl : true } ); 
        f.select ( { label: "Estensione", name: "fext", options: dl_manager._ext_list, force_select: false, style: "width:150px;" } );
        f.tags   ( { label: "Tags", name: "filters_tags", module: 'dl_manager', size: '20' } ); //dl_manager._module

        f.hidden ( '_module', 'dl_manager' );
        f.hidden ( '_title', 'DL Manager' );

        f.button ( { value: "Conferma", nonl : true, onclick: "jconed.render_plugin('jconed-dl-box-form')" } );

        f.button ( { value: "Preview", 'class': 'green', onclick: dl_manager.admin._jconed_dl_box_preview } );
        //f.button ( { value: "Reset", 'class': 'yellow', onclick: this.form.reset() } );
        
        f.workspace ( { name: 'jconed-dl-box-preview' } );
	
	//f.set ( jcontxt );
	jconed.toolbar.set_context ( f.get () );

	if ( frm_data )
	{
		f._fields.iterate ( function ( field )
			{
				if ( frm_data.get ( field ) )
					f.set_value ( field, frm_data.field );
			} );
	} 
};

dl_manager.admin._jconed_context = function ()
{
        var item = jconed.get_plugin_selection ();
        if ( ! item ) return;
        if ( ! liwe.dom.has_class ( item, 'liwe-plugin-dl_manager' ) ) return;

        pre = item.getElementsByTagName ( "pre" );
        if ( ! pre.length ) return;

        pre = pre [ 0 ];

        var values = null;
        eval ( 'values = ' + pre.innerHTML );

        dl_manager.admin._jconed_form ( values [ 'params' ] );
};

dl_manager.admin._jconed_dl_box_preview = function ()
{
        var f = liwe.form.get ( 'jconed-dl-box-form' );
        var params = f.get_values ();
        dl_manager.dl_box ( f.name );
};      
